<?php

namespace App\Api\V1\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $table = 'User';
    public  $timestamps = false;
    protected $primaryKey= 'user_id';

    public function addUserItem(array $data)
    {
        $data = [
            "username"=> "洒洒水",
            "password"=> "123456",
            "group_id"=> 1,
            "mobile"=> '13071297155',
            "nickname"=> "我是一个小程序员",
            "head_pic"=> "//host/head_pic.jpg",
            "sex"=> "男",
            "birthday"=> "1999-11-24",
            "create_time"=> time(),
            "update_time"=> time(),
        ];
        $this->username = $data['username'];
        $this->password = $data['password'];
        $this->group_id = $data['group_id'];
        $this->mobile = $data['mobile'];
        $this->nickname = $data['nickname'];
        $this->head_pic = $data['head_pic'];
        $this->sex = $data['sex'];
        $this->birthday = $data['birthday'];
        $this->create_time = $data['create_time'];
        $this->update_time = $data['update_time'];
        if ($this->save()){
            return $this->toArray();
        }

        return false;
    }

    /**
     * 编辑一个账号
     * @access public
     * @param array $data 外部数据
     * @return array|false
     */
    public function setUserItem(array $data)
    {
        $ds = [
            "user_id" => '17',
            "username" => 'carey',
            "mobile" => '888888',
            "is_mobile" => '0',
            "email" => 'admin@admin.cn',
            "is_email" => '0',
            "nickname" => 'carey',
            "head_pic" => '//host/head_pic.jpg',
            "sex" => '0',
            "birthday" => '1997-11-20',
            "user_level_id" => '1',
            "user_address_id" => '1',
            "group_id" => '3',
            "last_login" => '2018-04-22 00:23:00',
            "last_ip" => '127.0.0.1',
            "status" => '1',
            "create_time" => '2018-04-23 13:12:22',
            "update_time" => '2018-04-23 16:28:45	',
            "get_user_level" => 'adminalll',
            "get_user_money" => 'adminalll',
        ];
        $res = user::where('user_id',17)->update($ds);
        if ($res){
            return true;
        }
        return false;
    }


    /**
     * 修改一个账号密码
     */
    public function setUserPassword($ds)
    {
        $ds = [
            "group_id" => 2,
            "password" => '123456',
            "update_time"=> time()
        ];
        $res = admin::where('admin_id',14)->update($ds);
        if ($res){
            return $res;
        }
        return false;
    }

    /**
     * 获取一个账号
     * @access public
     * @param array $data 外部数据
     * @return array|false
     * @throws
     */
    public function getUserItem(array $data)
    {

        $res = admin::where('admin_id',14)->first()->toArray();
        if ($res){
            return $res;
        }

    }
    /**
     * 获取账号列表
     * @access public
     * @param array $data 外部数据
     * @return array|false
     * @throws
     */
    public function getUserList(array $data)
    {
        // 实际查询
        $res = admin::select()->limit('10');

        return json_encode($res);
    }

    /**
     * 批量删除账号
     * @access public
     * @param array $data 外部数据
     * @return array|false
     * @throws
     */
    public function delUserList(array $data)
    {
        $user_id = [21,22,23,24,25,26];
        $res = admin::where('user_id','$user_id')->delete();
        if ($res){
            return $res;
        }

    }

}
